package cn.edu.abtu.service.impl;

import cn.edu.abtu.client.UcenterClient;
import cn.edu.abtu.entity.EduComment;
import cn.edu.abtu.dao.EduCommentDao;
import cn.edu.abtu.exceptionhandler.GuliException;
import cn.edu.abtu.service.EduCommentService;
import cn.edu.abtu.utils.JwtUtils;
import cn.edu.abtu.vo.ResponseData;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class EduCommentServiceImpl implements EduCommentService {

    @Resource
    private EduCommentDao eduCommentDao;

    @Resource
    private UcenterClient ucenterClient;


    @Override
    public ResponseData add(EduComment eduComment, HttpServletRequest request) {
       try {
           String memberId = JwtUtils.getMemberIdByToken(request);
           if (StringUtils.isEmpty(memberId)){
               return ResponseData.fail("用户未登录,请先登录!OK?",101);
           }
           ResponseData infoById = ucenterClient.getMemberInfoById(memberId);
           Object data = infoById.getData();
           ObjectMapper objectMapper = new ObjectMapper();
           String str = objectMapper.writeValueAsString(data);
           Gson gson = new Gson();
           HashMap<String,Object> hashMap = gson.fromJson(str, HashMap.class);
           eduComment.setNickname((String) hashMap.get("nickname"));
           eduComment.setAvatar((String) hashMap.get("avatar"));
           eduComment.setGmtCreate(new Date());
           eduComment.setGmtModified(new Date());
           int insert = eduCommentDao.insert(eduComment);
           if (insert>0){
               return ResponseData.success("添加评论成功!",null);
           }else {
               return ResponseData.fail(20001,"添加评论失败",null);
           }
       }catch (Exception e){
           throw new GuliException(20001,"添加课程异常");
       }
    }

    @Override
    public ResponseData page(Integer page, Integer limit,String courseId) {
        Page pageInfo = new Page(page,limit);
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.orderByDesc("gmt_create");
        Page mapsPage = eduCommentDao.selectPage(pageInfo,queryWrapper);
        List<EduComment> records = mapsPage.getRecords();
        long current = mapsPage.getCurrent();
        long pages = mapsPage.getPages();
        long size = mapsPage.getSize();
        long total = mapsPage.getTotal();
        boolean hasNext = mapsPage.hasNext();
        boolean hasPrevious = mapsPage.hasPrevious();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("records",records);
        hashMap.put("current",current);
        hashMap.put("pages",pages);
        hashMap.put("size",size);
        hashMap.put("total",total);
        hashMap.put("hasNext",hasNext);
        hashMap.put("hasPrevious",hasPrevious);
        return ResponseData.success("查询成功",hashMap);
    }
}
